%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sajad Saeedi
% University of New Brunswick
% Copy Right 2012
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ref: 
% S. Saeedi, L. Paull, M. Trentini, M. Seto, and H. Li, 
% Map Merging Using Hough Peak Matching, 
% Proceedings of the IEEE/RSJ International Conference on 
% Intelligent Robots and Systems (IROS), 
% Vilamoura, Portugal, October 7-12, pp. 4683 - 4688.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% to (-1, 1) which standard for the occupancy grid map 
% pgm file is uint8. it should be converted to to int8
% to have normalization, it should be converted to double

function occ = pgm2occ(pgm)

[m n] = size(pgm);
occ = zeros(m,n);
for i = 1:m
    for j = 1:n
         if pgm(i,j) == 127 % unknown cells are catsed to 0
             occ(i,j) = 0;
         end
         
         if pgm(i,j) > 127  % free cells are casted to (0 1] range
             occ(i,j) = double(pgm(i,j)-127)/127;
         end
         
          if pgm(i,j) < 127 % occ cells are casted to (0 -1] range
            occ(i,j) = double(int8(pgm(i,j))-127)/127;
          end        
    end
end